package com.cqjtu.drive.service.impl;

import com.cqjtu.drive.mapper.RegisterRecordMapper;
import com.cqjtu.drive.mapper.RegistrationFormMapper;
import com.cqjtu.drive.mapper.StudentMapper;
import com.cqjtu.drive.pojo.RegisterRecord;
import com.cqjtu.drive.pojo.RegistrationForm;
import com.cqjtu.drive.pojo.Student;
import com.cqjtu.drive.response.Response;
import com.cqjtu.drive.service.RegisterRecordService;
import com.cqjtu.drive.service.RegistrationFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

@Service
public class RegistrationFormServiceImpl implements RegistrationFormService {

    @Autowired
    private RegistrationFormMapper registrationFormMapper;

    @Autowired
    private RegisterRecordService registerRecordService;

    @Autowired
    StudentMapper studentMapper;

    @Override
    public Response<String> insertRegistrationForm(Map<String,String> map) {
        RegistrationForm registrationForm = new RegistrationForm();
        try {
            List<RegistrationForm> list = registrationFormMapper.getAllRegistrationForms();
            if(list != null && !list.isEmpty()){
                String id = Integer.toString(Integer.parseInt(list.get(list.size() - 1).getrId()) + 1);
                registrationForm.setrId(id);
            }else {
                registrationForm.setrId("1");
            }
            registrationForm.setrAddress(map.get("rAddress")==null ? "0" : map.get("rAddress"));
            registrationForm.setrAge(Integer.parseInt(map.get("rAge")==null ? "0" : map.get("rAge")));
            registrationForm.setrName(map.get("rName")==null ? "0" : map.get("rName"));
            registrationForm.setrNative(map.get("rNative")==null ? "0" : map.get("rNative"));
            registrationForm.setrCertificate(map.get("rCertificate")==null ? "0" : map.get("rCertificate"));
            registrationForm.setrSex(map.get("rSex")==null ? "0" : map.get("rSex"));
            registrationForm.setrRace(map.get("rRace")==null ? "0" : map.get("rRace"));
            registrationForm.setrTel(map.get("rTel")==null ? "0" : map.get("rTel"));
            registrationFormMapper.insertRegistrationForm(registrationForm);

            //插入记录表
            RegisterRecord registerRecord = new RegisterRecord();
            String uid = map.get("uId");
            Student student = studentMapper.getStudentByuserId(uid);
            student.setsName(map.get("rName"));
            student.setsSex(map.get("rSex"));
            student.setsTel(map.get("rTel"));
            studentMapper.updateStudent(student);
            registerRecord.setsId(student.getsId());
            registerRecord.setrId(registrationForm.getrId());
            registerRecordService.insertRegisterRecord(registerRecord);

            return new Response<>(true, "Insert success", null);
        } catch (Exception e) {
            return new Response<>(false, "Insert failed: " + e.getMessage(), null);
        }
    }

    @Override
    public Response<String> updateRegistrationForm(RegistrationForm registrationForm) {
        try {
            registrationFormMapper.updateRegistrationForm(registrationForm);
            return new Response<>(true, "Update success", null);
        } catch (Exception e) {
            return new Response<>(false, "Update failed: " + e.getMessage(), null);
        }
    }

    @Override
    public Response<String> deleteRegistrationForm(String rId) {
        try {
            registrationFormMapper.deleteRegistrationForm(rId);
            return new Response<>(true, "Delete success", null);
        } catch (Exception e) {
            return new Response<>(false, "Delete failed: " + e.getMessage(), null);
        }
    }

    @Override
    public Response<List<RegistrationForm>> getAllRegistrationForms() {
        List<RegistrationForm> registrationForms = registrationFormMapper.getAllRegistrationForms();
        return new Response<>(true, "Success", registrationForms);
    }

    @Override
    public Response<RegistrationForm> getOne(String rId) {
        RegistrationForm registrationForm = registrationFormMapper.getRegistrationFormById(rId);
        if (registrationForm != null) {
            return new Response<>(true, "Student found",registrationForm);
        } else {
            return new Response<>(false, "Student not found");
        }
    }


}
